<?php

require_once('config_fj.php');
require_once('personalInfo.php');



$link = LinkMysql();
$varRegister=new MySession();
function sqlFilter($str,$search=false)
{
	global $link;
	$str=trim($str);
	if($search)
	$str=preg_replace("/_/","\_",$str);//不要想着字符串转义，不允许用户输入转义，就是这么任性
	return $link->real_escape_string($str);
}

// 获取发表时间
function getTime(&$base)
{
    extract($_GET);
	/**test
	$time="s t";
	$time_begin="2015-3-30";
	$time_end="2015-3-31";
	**/
    if (!empty($time)) {
        $condition = "";
		$time=sqlFilter($time);
        switch ($time) {
            case "all":
                $condition = "";
                break;
            case "lastUpdated":
                $condition .= empty($base)?"":" AND ";
                $condition .= "(发表时间   LIKE '%" . date("Y") . "%')";
                break;
            default:
				$year=explode('_',$time);
				if($year[0]>$year[1])
					return;
				$condition .= empty($base)?"":" AND ";
				$condition .= "  (发表时间 >= '$year[0]' AND 发表时间 <= '$year[1]-12-31')";
                break;
        }
        $base.=$condition;
    } 
}

// 获取领域 @par: area[、、、、]
function getArea(&$base)
{
	/*test
	$_GET['area']=array('net','computer');
	*/
    if (!empty($_GET['area'])) {
		$area=explode('-',$_GET['area']);
        $condition = "";
        foreach ($area as $value) {
            if (! empty($condition)) {
                $condition .= " OR ";
            }
			else{
			    $condition .= empty($base)?"":" AND ";
				$condition .="(";
			}
            $condition .= "领域 LIKE '%".$value . "%' ";
        }
		$condition.=empty($condition)?"":")";
		$base.=$condition;
    } 
}

// 获取原文语言 @par: paperlanguage[en,ch,all]
function getLanguage(&$base)
{
	/*test
	$_GET['paperlanguage']='中文';
	*/
    if (empty($_GET['paperlanguage']) || $_GET['paperlanguage'] == 'all') {
        return ;
    } 
	$_GET['paperlanguage']=sqlFilter($_GET['paperlanguage']);
    $base .= empty($base)?"":" AND ";
	$base .="(原文语言  LIKE '%" . $_GET['paperlanguage'] . "%')";
}

// 获取CCF等级 @par：CCFLevel
////返回值"" 或者 (LIKE .....)
function getCCFLevel()
{
	/*TEST
	$_GET['CCFLevel']=array('A','B','C');
	*/
    if (empty($_GET['CCFLevel']) || $_GET['CCFLevel'][0] == "all") {
        return "";
    } 
	else {    
		// Getting conference names
		// change : name level
		global $link;
		$sql = "";
		$ccflevel=explode('_',$_GET['CCFLevel']);
		foreach ($ccflevel as $val) {
			$sql .= empty($sql)?"select fullname from " . TBCONF . " where ":" OR ";
			$sql .= " CCFLevel = '$val' ";
		}
		//delete
		echo "CFSQ:".$sql.EOL;
		$ret = MysqlQuery($link, DBCONF, $sql);
		if(!$ret){
			//for debuging
			echo "At line:".__LINE__.".Query failed:".$link->error.EOL;
			return "";
		}
		$condition = "";
		while ($row = $ret->fetch_assoc()) {
			$row['fullname']=strtolower($row['fullname']);
			$condition .= empty($condition)?"(":" OR ";
			$condition .= " lower(期刊会议名)   LIKE '%" . sqlFilter($row['fullname'],true) . "%' ";
		}
		$condition .=empty($condition)?"":")";
		return $condition;
    }
}
// 获取影响因子 @infection[all ,1,2,3]
//返回值"" 或者 (LIKE .....)
function getInfection()
{
	/*TEST
	$_GET['infection']=array('1','3');
	*/

    if (empty($_GET['infection']) || $_GET['infection'][0] == "all") {
        return "";
    }
	
    // Getting conference names
    // change : name infection
    global $link;
    $sql = "";
	$infection=explode('_',$_GET['infection']);
    foreach ($infection as $val) {
		$val=intval($val);
        $infbegin=($val-1)*0.5;
		$infend=($val==3)?99999:$val*0.5;
		$sql .= empty($sql)?"select fullname from journals where(" : " OR( "; 
		$sql .= " impactFactors >=$infbegin && impactFactors <= $infend)";
    }
	//delete
	echo "sql:".$sql.EOL;
    $ret = MysqlQuery($link, DBCONF, $sql);
	if(!$ret){
		//for debuging
		echo "At line:".__LINE__.".Query failed:".$link->error.EOL;
		return "";
	}
    $condition = "";
    while ($row = $ret->fetch_assoc()) {
		$row['fullname']=strtolower($row['fullname']);
		$condition .= empty($condition)?"(":" OR ";
        $condition .= " lower(期刊会议名)  LIKE '%" . sqlFilter($row['fullname'],true)."%'";
    }
    $condition .= empty($condition)?"":")";
    return $condition;
}

/******************
 * JnalLevel[1,2,3,4,all]
 * 
 * 期刊分区 || CCF等级 || 影响因子
 * 
 * 
 * */

function getJnalConfinfo(&$base)
{
    global $link;
	/*test
	$_GET['JnalLevel']=array('1','2','3','4');
	*/
	$infection=getInfection();
	$conflevel=getCCFLevel();
	$jarea="";//期刊分区

    if(!empty($_GET['JnalLevel']) && $_GET['JnalLevel']!="all"){
		
		$sql="";
		$level_value=explode('_',$_GET['JnalLevel']);
		foreach($level_value as $val)
		{
			$sql.=empty($sql)?"select fullname from ".TBJNAL." WHERE ":" OR ";
			$sql.=" subregion LIKE '%".$val."%'";
		}
		//delete NEXT SENTENCE
		//echo "CONFSQL:".$sql.EOL;
		$ret=MysqlQuery($link,DBCONF,$sql);
		if(!$ret){
			//for debuging
			echo "At line:".__LINE__.".Query failed:".$link->error.EOL;
		}
		else{
			
			while($row=$ret->fetch_assoc())
			{
				$row['fullname']=strtolower($row['fullname']);
				$jarea.=empty($jarea)?"(":" OR ";
				$jarea.="lower(期刊会议名)  LIKE '%".sqlFilter($row['fullname'])."%'";
			}
			$jarea.=empty($jarea)?"":")";
		}
    }
    
	$condition="";
	if(empty($infection) || empty($jarea)){//如果有一个空，则条件直接相连
		$condition=$infection.$jarea;
	}
	else{
		$condition="( $infection AND $jarea )";
	}
	$condition.=empty($condition)?"":(empty($conflevel)?"":" OR $conflevel ");

	if(!empty($condition)){
		$base.=empty($base)?" ($condition) ":" AND ($condition)";
	}
	
}


function getSimpleSearchCond($key,$published=true,$logic="")
{
    $not="";
    if($logic=='NOT'){
        $not=" NOT ";
        $logic=" AND ";
    }
    else {
        $logic=" OR ";    
    }
	$key=strtolower($key);
	$key=sqlFilter($key,true);
	$condition="";
	$condition .= "(lower(标题)   $not LIKE '%$key%')";
	$condition .= "$logic (lower(摘要)      $not LIKE '%$key%')";
	$condition .= "$logic (lower(通信作者)   $not LIKE '%$key%')";
	if($published){
       
        $condition .= "$logic (lower(期刊会议名)   $not LIKE '%$key%')";
        // $condition .= "$logic (原文语言   $not LIKE '%$key%')";
        // $condition .= "$logic (发表时间   $not LIKE '%$key%')";
        // $condition .= "$logic (卷   $not LIKE '%$key%')";
        // $condition .= "$logic (期   $not LIKE '%$key%')";
        // $condition .= "$logic (页码   $not LIKE '%$key%')";      
        $condition .= "$logic (lower(关键词)   $not LIKE '%$key%')";
        $condition .= "$logic (领域   $not LIKE '%$key%')";
        $condition .= "$logic (DOI   $not LIKE '%$key%')";
        $condition .= "$logic (ISSN   $not LIKE '%$key%')";
        $condition .= "$logic (ISBN   $not LIKE '%$key%')";
        $condition .= "$logic (CN   $not LIKE '%$key%')";
        $condition .= "$logic (lower(基金)      $not LIKE '%$key%')";
        $condition .= "$logic (检索号  $not LIKE '%$key%')";       
        $condition .= "$logic ((lower(第一作者)   $not LIKE '%$key%')";//bracket #1---(
        if(empty($not) && $logic=='AND')
            $logic=' OR ';
        $condition .= "$logic (lower(第二作者)   $not LIKE '%$key%')";
        $condition .= "$logic (lower(第三作者)   $not LIKE '%$key%')";
        $condition .= "$logic (lower(第四作者)   $not LIKE '%$key%')";
        $condition .= "$logic (lower(第五作者)   $not LIKE '%$key%')";      
        $condition .= "$logic (lower(其他作者)   $not LIKE '%$key%'))";//bracket #1---)
	}
	else{
	    $condition .= "$logic (lower(所投期刊会议)      $not LIKE '%$key%')";
		$condition .= "$logic (lower(作者 )     $not LIKE '%$key%')";
	}
    return  $condition;
}

function hasLogged()
{
	global $browser_info;
	return $browser_info->permission!='visitor';
}

// 获取查询语句
// @par
//        key1 value1
// logic2 key2 value2
// .......
//后期扩展：LIKE 改为= 按想关性排序
function getBaseSql()
{

    $key_number = 10; // 可检索选项
    /*TEST
     $_GET=array("key1"=>"冯军","value1"=>"第一作者","logic2"=>"or","key2"=>"ywj","value2"=>"第二作者");*/
    extract($_GET);
    $condition= "";
    $i = 1;
    $logic1="";
    $queryTextLogic1="";
    for ($i=1;$i <= $key_number;++$i) {
        $key = "key" . $i;
        $value = "value" . $i;
        $logic = "logic" . $i;
        if (empty($$value)) {
            $value="queryText".$i;
            if(empty($$value))
                continue;
            $value="keyWords";
            $keyWords="queryText";
            $key = "queryText" . $i;
            $logic = "queryTextLogic" . $i;
        }
        $key=sqlFilter($$key,true);
        $value=sqlFilter($$value,true);
        $logic=sqlFilter($$logic,true);
        $not="";
        if($logic=='NOT'){
            $not=" NOT ";
            $logic=" AND ";
        }
        $condition .= empty($condition)?"(":" $logic (";//bracket #1---(
		$key=strtolower($key);
        switch($value){
            case '作者':
                if(empty($logic) || (empty($not) && $logic=='AND'));//防止 空逻辑  当作者是并且的时候，里面的作者应该是或
                $logic=" OR ";
                 
                $condition .= "(lower(第一作者)   $not LIKE '%$key%')";
                $condition .= "$logic (lower(第二作者)   $not LIKE '%$key%')";
                $condition .= "$logic (lower(第三作者)   $not LIKE '%$key%')";
                $condition .= "$logic (lower(第四作者)   $not LIKE '%$key%')";
                $condition .= "$logic (lower(第五作者)   $not LIKE '%$key%')";
                $condition .= "$logic (lower(通信作者)   $not LIKE '%$key%')";
        $condition .= "$logic (lower(其他作者)   $not LIKE '%$key%'))";////bracket #1---)

        break;
            case '刊名':
                $condition .= "lower(期刊会议名)   $not LIKE '%$key%' AND 论文类型  LIKE '%期刊%')";//bracket #1---)
                break;
            case '会议名':
                $condition .= "lower(期刊会议名)   $not LIKE '%$key%' AND 论文类型 LIKE '%会议%')";//bracket #1---)
                break;
            //don't change the order of the case
			case '标题':
            case '摘要':  
			case 'ISSN':
            case 'CN':	
			case 'ISBN':		
		    case '关键词':
            case '基金':
            case '检索号':
                $condition .= "lower($value)  $not LIKE '%$key%')";//bracket #1---)
				if($value=='ISSN'  || $value=='CN'){
					$condition .=" AND (论文类型 LIKE '%期刊%')";
				}
				if($value=='ISBN'){
					$condition .=" AND (论文类型 LIKE '%会议%') ";
				}
                break;
            case 'queryText':
                 
                if(!empty($not))
                    $logic='NOT';
                 $condition .= getSimpleSearchCond($key,true,$logic).")";//bracket #1---)
            default:
                break;
        }
    }
 
    getTime( $condition);
    getArea($condition);
    getLanguage($condition);
    getJnalConfinfo($condition);

    return empty($condition)?false:("SELECT * FROM ".TBLABA." WHERE ".$condition);	
     
}

//获取翻页的sql语句以及检索的总条数
//需增加变量，$_GET['action']的值为select_page

function getFullSql($order)
{
    global $link;
    $count=0;
    $default_per_page_count=5;//没页默认的显示数目
    $sql=getBaseSql();
    @session_start();
	if(!empty($sql)){//增加一个session变量吧，免得重复检索数量
	  
	  if(empty($_SESSION['new_search']) || $_SESSION['new_search']){//如果是从搜索界面进入当前界面的
		 
		  $ret=MysqlQuery($link,DBLABA,$sql);
		  if($ret){
			  $count=$ret->num_rows;
			  $_SESSION['number_of_pub']=$count;
			  $_SESSION['new_search']=false;
		  }
		  else{
				echo "Sql:".$sql.EOL;
				echo "Query failed:".$link->error.EOL;
				return 0;
		  }
	  }

	}
	else{
		return 0;
	}
	
	$sql_backup=$sql;//用于导出的sql语句
	$sql_backup.=$order;
	
	$page_index=empty($_GET['page_index'])?1:$_GET['page_index'];
	$number_per_page=empty($_GET['number_per_page'])?$default_per_page_count:$_GET['number_per_page'];
	$page_count=ceil($count/$number_per_page);//向上取整
	
	$start_index=($page_index-1)*$number_per_page;
	if($start_index>=$_SESSION['number_of_pub']){
	        return 0;
	}
	else {
	    $sql.=$order." LIMIT $start_index,$number_per_page";
	}
	return array('count'=>array($page_count,$count),'sql'=>$sql,'sql_export'=>$sql_backup);
}

//显示一篇论文
//cont：array include the info of a paper
function getHtmlContent($cont,$type,&$content)
{
	$order=array('一','二','三','四','五','六','七','八','九','十');
	$search_paper = ($type=='C')?"Ach_conference.html?":($type=='J'?"Ach_journal.html?":"#");
	if($search_paper != '#')
	$search_paper .= "title=".$cont['标题']."&author=".$cont['第一作者'];
	//title
	$content.="
	<div style='margin-left:auto;margin-right:auto;width:700px;background:;z-index:10px;padding-top:3px;'>
	<li class='noAbstract'  >\n			
	<div class='header'>\n
		<div class='select'>\n
			<input name='' title='Select this article: ".$cont['标题']."' type='checkbox' value='' id='5704884' />
		</div>
		<div class='detail'>
		<script>
		/**
		 * parameters needed to create authorlinks on document load
		 * search.min.js
		 */
		var searchPropertiesParamSearchWithin = 'searchWithin';
		var searchPropertiesParamNewSearch = 'newsearch';
		var authorsGetReference = 'p_Authors';
		var authorIdReference = 'p_Author_Ids';
		</script>
		<h3>\n           
			<a  href='$search_paper' target='_blank'>\n
							".$cont['标题']."\n
			</a>\n
		</h3>\n";
	//author
	foreach($order as $val){
		$val="第".$val."作者";
		if(!empty($cont[$val])){
			if($val!='第一作者')
			$content .= ';';
			$content .="
			<a href='#'  class='uthorPreferredName prefNameLink' title='' >\n
			<span id='authorAffiliations' class='State Key Lab. of Networking &amp; Switching Technol., Beijing Univ. of Posts &amp; Telecommun., Beijing, China|c|'></span>\n 	
			<span id='preferredName' class='".$cont[$val]."'></span>\n
			<span id='authorId' class='37857706300'></span>	\n	
			".$cont[$val]."</a>\n";
		}
	}
	if(!empty($cont['通信作者'])){
		$content.="
			<a href='#'  class='uthorPreferredName prefNameLink' title='' >\n
			<span id='authorAffiliations' class='State Key Lab. of Networking &amp; Switching Technol., Beijing Univ. of Posts &amp; Telecommun., Beijing, China|c|'></span>\n 	
			<span id='preferredName' class='".$cont['通信作者']."'></span>\n
			<span id='authorId' class='37857706300'></span>	\n	
			;".$cont['通信作者']."</a>\n";
	}
	if($type!='U'){////Changed by fj 
	//期刊、会议名
		if(!empty($cont['期刊会议名'])){
			$content.=" <a href='../xpl/mostRecentIssue.jsp?punumber=5640191'>\n
			                	<br/>".$cont['期刊会议名']."\n
			            </a>\n";
		}
		//DOI
		$content .= "<br/>DOI:\n";	
		$content.=empty($cont['DOI'])?" 暂无 ":"<a href='http://dx.doi.org/".$cont['DOI']."target='blank'>\n
											  ".$cont['DOI']." </a>\n";
		//publication year
		$content .= "<br/>发表时间: ";
		$content .= empty($cont['发表时间'])?" 暂无 ":$cont['发表时间'];
		if($type=='C'){
		//type
		$content .="<p>
			<span class='type'>
			<br/>	会议论文
			</span>                        	
			</p>
			</div>";
		} 
		else{
			//page
			$content .= " , 页码: ";
			$content .= empty($cont['页码'])?" 暂无 ":$cont['页码'];
			//type
			$content .="<p>
			<span class='type'>
			<br/>	期刊论文
			</span>                        	
			</p>";
		}
	}
	else{////Added by fj 
		//type
		$content .="<p>
		<span class='type'>
		<br/>	此论文尚未发表，当前状态:";
		$content.=empty($cont['当前状态'])?"无法获取":$cont['当前状态'];
		$content.=
		"
		</span>                        	
		</p>";
	}
	    //abstract
		$content .="<p class='links'>
						 		<span class='floatLeft'>";
		$content .=empty($cont['摘要'])?"暂无摘要":"
					 		    		 <a href='#'  onclick='return false' class='quick-ab-button RevealControl'><img src='http://staticieeexplore.ieee.org/assets/img/btn.quick-abstract.collapsed.gif'/>摘要</a>
					 		    		&nbsp;|";
	   if($type!='U'){
    	   //全文下载
    	   $content .= empty($cont['全文下载'])?"暂无全文":"<a href='".$cont['全文下载']."'><img class='button' src='http://staticieeexplore.ieee.org/assets/img/iconPdf.png' alt='PDF file icon' title='Download or View the PDF' />PDF</a>&nbsp;
    								";
	   }
	   $content .="</span></p>";

		//content of abstract
		$content .= empty($cont['摘要'])?"":"<div  style='border:solid 1px red;width: auto; overflow: visible;' class='abstract RevealContent' ><p>".$cont['摘要']."</p>";
		$content.="</div>
			</div>		
		</div>
		";
}

/*function exportToTable($cont,$paper_type,$export_type)
{
	echo "Export,paper_type:".$paper_type.EOL."export_type:".$export_type.EOL;
}
*/
//格式化论文，以便在网页上显示或者导出到word
function formatPublishedPaper(&$row,$dis=true,$lang='C')
{
	$number=array('一','二','三','四','五');
	for($i=0;$i<5;++$i)//格式化作者
	{
		$index="第".$number[$i]."作者";
		if(empty($row[$index]))
			break;
		$both_author=explode('__',$row[$index]);//获取英文、中文作者名
		if($lang=='E')
		$row[$index]=empty($both_author[0])?$both_author[1]:$both_author[0];//如果没有英文的显示中文
		else
		$row[$index]=empty($both_author[1])?$both_author[0]:$both_author[1];//如果没有中文的显示英文
	}
	if(!empty($row['其他作者'])){
		$authors=explode("，",$row['其他作者']);
		if($dis){
			$number=array('六','七','八','九','十');
			for($i=0;$i<count($authors) && $i<count($number);++$i)
			{
				$index="第".$number[$i]."作者";
				$both_author=explode('__',$authors[$i]);//获取英文、中文作者名
				if($lang=='E')
				$row[$index]=empty($both_author[0])?$both_author[1]:$both_author[0];//如果没有英文的显示中文
				else
				$row[$index]=empty($both_author[1])?$both_author[0]:$both_author[1];//如果没有中文的显示英文
			}
		}
		else{
			$row['其他作者']="";//导出的时候获取其他作者信息
			foreach($authors as $val)
			{
				$val=explode('__',$val);
				if($lang=='E')
				$row['其他作者'].=empty($val[0])?$val[1]:$val[0];//如果没有英文的显示中文
				else
				$row['其他作者'].=empty($val[1])?$val[0]:$val[1];//如果没有中文的显示英文
			}
		}
	}
	//通信作者
	if(!empty($row['通信作者'])){
		$both_author=explode('__',$row['通信作者']);
		if($lang=='E')
		$row['通信作者']=empty($both_author[0])?$both_author[1]:$both_author[0];
		else
		$row['通信作者']=empty($both_author[1])?$both_author[0]:$both_author[1];
	}
	$both_title=explode('__',$row['标题']);//标题
	if($lang=='E')
	$row['标题']=empty($both_title[0])?$both_title[1]:$both_title[0];
	else
	$row['标题']=empty($both_title[1])?$both_title[0]:$both_title[1];

	if(!empty($row['期刊会议名'])){//期刊会议名
		$both_mag_name=explode('__',$row['期刊会议名']);
		if($lang=='E')
		$row['期刊会议名']=empty($both_mag_name[0])?$both_mag_name[1]:$both_mag_name[0];
		else
		$row['期刊会议名']=empty($both_mag_name[1])?$both_mag_name[0]:$both_mag_name[1];
	}

	if(strstr($row['论文类型'],'会议') && !empty($row['发表时间'])){
		$dates=explode('-',$row['发表时间']);
		//年
		if(!empty($dates[0])){
		$row['发表时间']=$dates[0];
		$row['会议时间']="$dates[0]年";
			if(!empty($dates[1])){
				//月
				$row['会议时间'].="$dates[1]月";
					if(!empty($dates[2])){
						//日
						$row['会议时间'].=empty($dates[2])?"":"$dates[2]";//开始
							if(!empty($dates[3])){
								$row['会议时间'].="-$dates[3]日";//结束
							}
							else{
								$row['会议时间'].="日";
							}
					}
			}
		}
	}
	
	//基金
	if(!empty($row['基金'])){
		$row['基金']=preg_replace('/__/'," , ",$row['基金']);
	}
}

//获取数据库里面所有满足检索条件的期刊或者会议论文
function getPapersFromMysql($sql,&$content,$published=true,$dis=true,$export_type="",$lang='C')
{
    global $link;
    $ret=MysqlQuery($link,DBLABA,$sql);
    if(!$ret){
        //for debuging
		echo $sql.EOL;
        echo "At line:".__LINE__.".Query failed:".$link->error.EOL;
		return 0;
    }
	$count=$ret->num_rows;
    while($row=$ret->fetch_assoc())
    {
        if($published){
            $type=strstr($row['论文类型'],'会议')?"C":"J";
			formatPublishedPaper($row,$dis,$lang);
        }
        else{
            $type="U";//unpublished
            $number=array('一','二','三','四','五','六','七','八','九','十');
            $authors=explode('，',$row['作者']);
            for($i=0;$i<count($authors) && $i<count($number);++$i)
    		{
    				$index="第".$number[$i]."作者";
    				$row[$index]=$authors[$i];
    		}
        }
		if($dis)
			getHtmlContent($row,$type,$content);
		else if(!empty($export_type)){//用于导出excel，因为键值要作为列标，所以不能有两个会议时间
			if(strstr($row['论文类型'],'会议') && !empty($row['发表时间'])){
				$row['发表时间']=$row['会议时间'];
				unset($row['会议时间']);
			}
			exportToTable($row,$export_type,false);
		}
    }
	if(!empty($export_type))//写完
			exportToTable("",$export_type,true);

	return $count;
}



//@par order type
//order[1,2,3,4,5,6,7,8,9]
//标题[A-Z]、标题[Z-A],最新发表在前、较晚发表在前、被引次数增、被引次数减，会议在前、期刊在前、未发表在前

function getWebContent(&$content)
{
	$order="";
	/*test*/
	//$_GET['order']=9;
	if(!empty($_GET['order'])){
		$order=" ORDER BY ";
		$_GET['order']=sqlFilter($_GET['order']);
		switch($_GET['order'])
		{
			case '1':
				$order .= " 标题 ASC";
			break;
			case '2':
				$order .= " 标题 DESC";
			break;
			case '3':
				$order .= " 发表时间 DESC";
			break;
			case '4':
				$order .= " 发表时间 ASC";
			break;
			case '5':
				$order .= " 被引次数 ASC";
			break;
			case '6':
				$order .= " 被引次数 DESC";
			break;
			case '7':
				$order .= "  论文类型 ASC ";
			break;
			case '8':
				$order .= " 论文类型 DESC ";
			break;
			default:
				$order="";
			break;
		}
	}
	$ret=getFullSql($order);
	if($ret){
      
    	  $sql=$ret['sql'];
    	  //for debuging
    	  echo "You searched for:<font color='red'>".$sql.EOL."Page:".$ret['count'][0].EOL."</font>".EOL;
    	  $content="";
    	  if(!empty($sql)){
    		  getPapersFromMysql($sql,$content);
			  
    	  }
		 
    	  $content.="<br/>";

          return array('pagecount'=>$ret['count'][0],'total_results'=>$ret['count'][1],'export_sql'=>$ret['sql_export']);
    }
    else{
        //for debuging
        //echo "invalid searching choices".EOL;   
		 return array('pagecount'=>0,'total_results'=>0,'export_sql'=>"");
    }
    
   
}

//获取所有的期刊论文
	function getAllPapers(&$content,$type)
	{	
		if($type=='J')
			$sql="SELECT * FROM ".TBLABA." WHERE 论文类型 LIKE '%期刊%' ORDER BY 发表时间 DESC";
		else
			$sql="SELECT * FROM ".TBLABA." WHERE 论文类型 LIKE '%会议%' ORDER BY 发表时间 DESC";
		//function getPapersFromMysql($sql,&$content,$published=true,$dis=true,$export_type="",$lang='C')
		global $link;
		$ret=MysqlQuery($link,DBLABA,$sql);
		if(!$ret){
			//for debuging
			echo $sql.EOL;
			echo "At line:".__LINE__.".Query failed:".$link->error.EOL;
			return 0;
		}
		$count=0;
		while($row=$ret->fetch_assoc())
		{
			$count+=1;
			formatPublishedPaper($row);
			$ret_info=simpleDisStyle($row,$type);
			$id_temp=$ret_info['id'];
			$content .="<div> ";
			$content .= "<p><a href='".$ret_info['link']."' target='_blank'>[$count] ".$ret_info['info']."</a>　";
			if(!empty($row['摘要'])){
				$row['摘要']=nl2br($row['摘要']);
				$content .= "<a onclick=hdShowAbs('$id_temp')><img  src='./img/abstract.gif'/></a><div id='$id_temp' style='display:none;margin-left:12px;'>　　".$row['摘要']."</div>";
			}
			$content .= "</p></div>";	
		}
		return $count;
	}

	//获取一篇论文要显示的内容，如果出错直接终止
	function searchSelectedPaper($type_info,$unpub=false){
		
		global $link;
		
		$table=$unpub?TBIDEN:TBLABA;
		if(!empty($_GET['title']) && !empty($_GET['author'])){
			
			$sql="SELECT * FROM ".$table." WHERE 标题 LIKE '%".sqlFilter($_GET['title'])."%' AND 第一作者 LIKE '%".sqlFilter($_GET['author'])."%' LIMIT 1";
			//getPapersFromMysql($sql,&$content,$published=true,$dis=true,$export_type="",$lang='C')
			$ret=MysqlQuery($link,DBLABA,$sql);
			if(!$ret){
					//for debuging
					echo $sql.EOL;
					echo "At line:".__LINE__.".Query failed:".$link->error.EOL;
					
			}else if($cont=$ret->fetch_assoc()){
				//formatPublishedPaper(&$row,$dis=true,$lang='C')
				formatPublishedPaper($cont);
				if(strstr($cont['论文类型'],$type_info))//如果不是指定类型的论文，则显示失败
				return $cont;
			}
		}
		
		echo "貌似出了什么问题，我们不能为你显示请求的页面，请关闭当前页面.....";//防止直接调至此页面
		exit;
	}
	//简单显示一篇论文，没有摘要等等 
	//@return 格式后的内容
	function simpleDisStyle($row,$type)
	{
		
		$cont="";
		$number=array('一','二','三','四','五','六','七','八','九','十');
		//author
		foreach($number as $val){
			$val="第".$val."作者";
			if(!empty($row[$val])){
				$row[$val]=trim($row[$val]);
				if($val=='第一作者'){
					$cont.=$row[$val];
				}
				else{
					$cont.=",".$row[$val];
				}
			}
			else{
				break;
			}
		}
		//title
		$cont.=".".$row['标题'];
		
		//期刊会议名
		$cont.=empty($row['期刊会议名'])?"":(".".$row['期刊会议名']);
		
		//会议地点，时间
		if($type=='C'){
			$cont .= empty($row['会议地点'])?"":".".$row['会议地点'];	
			$cont .= empty($row['会议时间'])?"":".".$row['会议时间'];	
		}
		
		//卷期时间
		if($type=='J'){
			$cont .= empty($row['卷'])?"":".".$row['卷']." 卷";
			$cont .= empty($row['期'])?"":(empty($row['卷'])?".".$row['期']." 期 ":",".$row['期']." 期");
			$cont .= empty($row['发表时间'])?"":".".$row['发表时间'];
		}

		//页码

		$cont .= empty($row['页码'])?"":".pp: ".$row['页码'];
		if(strstr($row['论文类型'],"期刊")){
			$detail_info_link="Ach_journal.html?title=".$row['标题']."&author=".$row['第一作者'];
		}
		else{
			$detail_info_link="Ach_conference.html?title=".$row['标题']."&author=".$row['第一作者'];
		}
		//id
		$id_temp=hash('adler32',$row['标题'].$row['第一作者']);
		return array("info"=>$cont,"link"=>$detail_info_link,"abstract"=>$row['摘要'],"id"=>$id_temp);	
	}
?>
